package com.OnTimeTeam.OnTime;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.ArrayList;

import android.app.Activity;
import android.app.Dialog;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.TimePicker;
import android.widget.AdapterView.OnItemClickListener;

public class RouteDetails extends Activity{

	private SmartAlarmFeatures af;
	private ListView routeList;
	private RouteDetailsList adapter;
	private Button navigateButton;
	
	private TextView origin;
	private TextView dest;

	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.show_route_details); //View from file "res/layouts/reminders.xml"

		navigateButton = (Button)findViewById(R.id.navigateButton);
		routeList = (ListView)findViewById(R.id.routeDetailsList);
		
		navigateButton.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				
				try {
					String origin = URLEncoder.encode(getIntent().getExtras().getString("Origin"), "UTF-8");
					String dest = URLEncoder.encode(getIntent().getExtras().getString("Dest"), "UTF-8");
					
					if(getIntent().getExtras().getString("Transport").equals("Vehicle")) {
						startActivity(new Intent(android.content.Intent.ACTION_VIEW, 
							    Uri.parse("http://maps.google.com/maps?saddr=" + origin + "&daddr=" + dest + "&dirflg=d")));
					}
					else if(getIntent().getExtras().getString("Transport").equals("Transit") == false) {
						startActivity(new Intent(android.content.Intent.ACTION_VIEW, 
							    Uri.parse("http://maps.google.com/maps?saddr=" + origin + "&daddr=" + dest + "&dirflg=w")));
					}
					else	
					{
						startActivity(new Intent(android.content.Intent.ACTION_VIEW, 
							    Uri.parse("http://maps.google.com/maps?saddr=" + origin + "&daddr=" + dest + "&dirflg=r")));

					}

					
				
				} catch (UnsupportedEncodingException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		});
		
		origin = (TextView)findViewById(R.id.tvFrom);
		dest = (TextView)findViewById(R.id.tvTo);
		
		origin.setText(getIntent().getExtras().getString("Origin"));
		dest.setText(getIntent().getExtras().getString("Dest"));
		
		
/*
 * 
		routeList.setOnItemClickListener(new OnItemClickListener() {
			
			public void onItemClick(AdapterView<?> arg0, View v, int index, long id) {				
				switch(index){
				case 0:
					
					showTimePicker();
					
					break;
				}
			}
		});
*/		
		ArrayList<Boolean> items = new ArrayList<Boolean>();
		for(int i = 0; i < 5; ++i)
			items.add(true);
		
		af = new SmartAlarmFeatures();
				
		af.setArrivalHour(getIntent().getExtras().getString("ArrivalHour"));
		af.setArrivalMinute(getIntent().getExtras().getString("ArrivalMinute"));
		af.setHour(getIntent().getExtras().getString("Hour"));
		af.setMinute(getIntent().getExtras().getString("Minute"));
		af.setDest(getIntent().getExtras().getString("Dest"));
		af.setOrigin(getIntent().getExtras().getString("Origin"));
		af.setTTO(getIntent().getExtras().getString("TTO"));
		af.setTransport(getIntent().getExtras().getString("Transport"));
		
		String duration = getIntent().getExtras().getString("Duration");
		String distance = getIntent().getExtras().getString("Distance");

		
		//create adapter for the list
		adapter = new RouteDetailsList(this,R.layout.route_item, items, af, duration, distance, true);
		routeList.setAdapter(adapter);
		routeList.setTextFilterEnabled(true);
		
		
		
	}

	
	void showTimePicker()
	{
		
		final LayoutInflater inflater = (LayoutInflater) RouteDetails.this.getSystemService(LAYOUT_INFLATER_SERVICE);
		View timeLayout = inflater.inflate(R.layout.time_picker,null);
		
		Button bCancelTime = (Button)timeLayout.findViewById(R.id.bCancel);
		Button bSaveTime = (Button)timeLayout.findViewById(R.id.bSave);
		final TimePicker timePicker = (TimePicker) timeLayout.findViewById(R.id.timePicker);
		
		
		final Dialog timeDialog = new Dialog(RouteDetails.this, R.style.GrowBottom);
		timeDialog.requestWindowFeature(Window.FEATURE_LEFT_ICON);
		timeDialog.setContentView(timeLayout);
		timeDialog.setFeatureDrawableResource(Window.FEATURE_LEFT_ICON, R.drawable.time_icon);
		timeDialog.setTitle(getResources().getString(R.string.wakeUpAt));
		timeDialog.setCancelable(true);
		timeDialog.setCanceledOnTouchOutside(false);
		
		bCancelTime.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				timeDialog.dismiss();
			}
		});
			
		bSaveTime.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				
				timePicker.clearFocus();
				
            	//get hour and minute in from timePicker object
            	int hour = timePicker.getCurrentHour();
            	int minute = timePicker.getCurrentMinute();
            	
            	//set hour and minute in alarm-features object
            	if(hour <= 9)
            		af.setHour("0" + Integer.toString(hour));
            	else
            		af.setHour(Integer.toString(hour));
            	
            	if(minute <= 9)
            		af.setMinute("0" + Integer.toString(minute));
            	else
            		af.setMinute(Integer.toString(minute));

            	adapter.notifyDataSetChanged();
            	
            	timeDialog.dismiss();
				
			}
		});

		timePicker.setIs24HourView(true);//Set to 24 hours view (without the AM/PM mode)
		
		//set hour and minute in TimePicker
		timePicker.setCurrentHour(Integer.valueOf(af.getHour()));
		timePicker.setCurrentMinute(Integer.valueOf(af.getMinute()));

		timeDialog.show();
		
	}


	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
	}

	@Override
	protected void onPause() {
		// TODO Auto-generated method stub
		super.onPause();
	}

	@Override
	protected void onRestart() {
		// TODO Auto-generated method stub
		super.onRestart();
	}

	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
	}

}
